﻿using System;
using System.Data;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Web.Workflow.Services;

namespace H2.NET.Web.Management.Permission
{
    public partial class Create : BasePage
    {
        private readonly PermissionService _permissionService = new PermissionService();

        public string ParentID = string.Empty;
        public string Module = string.Empty;
        public string span_type = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            var action = Request.QueryString["action"];
            if (action == "create")
            {
                DoCreate();
            }
            else
            {
                InitData();
            }
        }

        private void InitData()
        {
            var querystring = Request.QueryString["id"];

            //判断是否选中父级
            if (string.IsNullOrEmpty(querystring)) return;

            var id = Convert.ToInt32(querystring);

            var model = _permissionService.GetModel(id);

            ParentID = model.Id.ToString();
            Module = model.Module;
            ParentName.Text = model.Name;
        }

        private void DoCreate()
        {
            var names = (Request.Form["Name"] ?? "").Split(',');
            var titles = (Request.Form["Title"] ?? "").Split(',');
            var urls = (Request.Form["Url"] ?? "").Split(',');
            var relCodes = (Request.Form["RelCode"] ?? "").Split(',');
            var modules = (Request.Form["Module"] ?? "").Split(',');
            var types = (Request.Form["Type"] ?? "").Split(',');
            var classes = (Request.Form["Class"] ?? "").Split(',');
            var orders = (Request.Form["Order"] ?? "").Split(',');
            var FISLC = (Request.Form["FISLC"] ?? "").Split(',');
            var FDataName = (Request.Form["FDataName"] ?? "").Split(',');

            var dataTable = new DataTable("Permission");

            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Title");
            dataTable.Columns.Add("Url");
            dataTable.Columns.Add("RelCode");
            dataTable.Columns.Add("Module");
            dataTable.Columns.Add("Type");
            dataTable.Columns.Add("Class");
            dataTable.Columns.Add("Order");
            dataTable.Columns.Add("ParentID");
            dataTable.Columns.Add("FISLC");
            dataTable.Columns.Add("FDataName");

            for (var i = 0; i < names.Length; i++)
            {
                var row = dataTable.NewRow();
                row["Name"] = names[i];
                row["Title"] = titles[i];
                row["Url"] = urls[i];
                row["RelCode"] = relCodes[i];
                row["Module"] = modules[i];
                row["Type"] = types[i];
                row["Class"] = classes[i];
                row["Order"] = orders[i];
                row["FISLC"] = FISLC[i];
                row["FDataName"] = FDataName[i];
                if (!string.IsNullOrEmpty(Request.Form["ParentID"]))
                    row["ParentID"] = Request.Form["ParentID"];

                dataTable.Rows.Add(row);
            }

            _permissionService.BulkCopyDataTable(dataTable, "Permission");

            SuccessMsg("PermissionIndex");
        }
    }
}